import Router from '../../utils/router';
import base from '../../config/base.js';
import service from '../../services/conch';
var scrollTime = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    icon_top_bg: base.static + 'conch/shellfish-top-bg.png',
    loadAllBloon: true,
    endAllBloon: true,
    goodsList: [],
    page: 0,
    store_id: '',
    user_id: '',
    nomore: true, //没有更多了
    transmit: {
      style: 'background-image:linear-gradient(-180deg, #FFF6D7 0%, #FFE46F 80%);position:absolute;right:0rpx;top:30rpx;width:120rpx;height:50rpx; line-height:50rpx;opacity:1;text-align:center;font-size:26rpx;color:#FE3F3E;border-top-left-radius:36rpx;border-top-right-radius:0;border-bottom-left-radius:36rpx;border-bottom-right-radius:0;padding:0;border:0;',
      value: '赚贝壳'
    },
    browsegoods: false,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let user_info = wx.getStorageSync('user_info') || {id:'',lastView:{seid:''}}; 
    that.setData({
      store_id: user_info.lastView.seid,
      user_id: user_info.id,
    })
    that.getConch(1);
    if (options.browsegoods && options.browsegoods == "browsegoods"){
      // 任务中心进入做任务
      that.setData({
        browsegoods: true
      })
      that.seeGoodsTimeOut()
    }
  },
  //2.9贝壳专区列表
  getConch: function (page) {
    let that = this;
    let params = {
      limit: 20,
      offset: page
    }
    service.getConchGoods(params, function (res) {
      if (res.code && res.code == 200) {
        let strarr = that.data.goodsList.concat(res.data.list);
        that.setData({
          goodsList: strarr,
          endAllBloon: true,
          page: page + 1,
        })
        if (strarr.length == res.data.count){
          that.setData({
            loadAllBloon: false,
            nomore: false
          })
        }
      }
      wx.stopPullDownRefresh()
    });
  },
  goodsTimer: null,
  seeGoodsTimeOut: function(){
    let that = this;
    if (that.goodsTimer) {
      clearTimeout(that.goodsTimer)
    };
    that.goodsTimer = setTimeout(() => {
      let params = {}
      service.browserGoodsMine(params, function (res) {
        if (res.code && res.code == 1) {
          wx.showToast({
            title: '任务完成',
            icon: 'none',
          })
        }
      });
    }, 60000);
  },
  //监听页面高度
  scrollTimer: null,
  onPageScroll: function (e) {
    let that = this;
    if (that.scrollTimer) {
      clearTimeout(that.scrollTimer)
    };
    that.scrollTimer = setTimeout(() => {
      that.setData({
        scrolltop: e.scrollTop
      })
    }, 500);
  },
  //去任务中心
  goTaskCenter: function(){
    Router.go('dailytasks')
  },
  //返回顶部
  goscoltop: function () {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let that = this;
    if (that.goodsTimer) {
      clearTimeout(that.goodsTimer)
    };
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that = this;
    if (that.scrollTimer) {
      clearTimeout(that.scrollTimer)
    };
    that.setData({
      loadAllBloon: true,
      endAllBloon: true,
      goodsList: [],
      page: 0,
    });
    that.getConch(1);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    if (that.data.loadAllBloon && that.data.endAllBloon) {
      that.setData({
        endAllBloon: false
      })
      that.getConch(that.data.page);
    }
  },
})